using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Crossborder.Common.Utility.HttpHelper;
using Crossborder.Core.Const;
using Crossborder.Core.Dto;
using Crossborder.Extensions.Filters;
using Crossborder.IService;
using Microsoft.AspNetCore.Mvc;

namespace Crossborder.Web.MinimalApi.Endpoint
{
    public static class ShoppingCartEndpoint
    {
        public static void UseShoppingCartEndpoint(this WebApplication app)
        {
            var gorup = app.MapGroup("api/ShoppingCart/")
            .WithTags("ShoppingCart")
            .RequireAuthorization(AuthorizationConst.USER)
            .AddEndpointFilter<TokenFilter>();

            gorup.MapGet("GetShoppingCart", async (IShoppingCartService service, HttpContext context) =>
            {
                var userId = context.Items["UserId"]?.ToString();
                if (string.IsNullOrWhiteSpace(userId))
                {
                    return HttpHelper.Fail("Token已过期，验证失败！");
                }
                var result = await service.GetShoppingCart(userId);
                return HttpHelper.Success("查询成功", result);
            });

            gorup.MapPost("SetShoppingCart", async ([FromBody] ShoppingCartInputDto dto, IShoppingCartService service, IProductService proService, HttpContext context) =>
            {
                var userId = context.Items["UserId"]?.ToString();
                if (string.IsNullOrWhiteSpace(userId))
                {
                    return HttpHelper.Fail("Token已过期，验证失败！");
                }
                await service.SetShoppingCart(userId, dto);
                return HttpHelper.Success("添加成功！");
            });

        }
    }
}